package com.franklin.ideaplugin.api.ui.editor;

import com.franklin.ideaplugin.api.icon.ApiIcons;
import com.franklin.ideaplugin.api.ui.IBaseComponent;
import com.franklin.ideaplugin.api.utils.ProjectUtils;
import com.franklin.ideaplugin.easytesting.icon.PluginIcons;
import com.franklin.ideaplugin.easytesting.ui.Sizes;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.ui.popup.*;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

/**
 * @author Ye Junhui
 * @since 2023/7/12
 */
public interface IBaseSoftWrapEditor extends IBaseComponent {

    /**
     * 初始化自动换行按钮
     * @param customEditor
     * @param softWrapBtn
     */
    default void initSoftWrapButton(CustomEditor customEditor, JButton softWrapBtn){
        softWrapBtn.setIcon(ApiIcons.SOFT_WRAP_ICON);
        softWrapBtn.setText("Soft Wrap");
        softWrapBtn.setToolTipText("Soft Wrap");
        softWrapBtn.addActionListener(e -> {
            Editor editor = customEditor.getEditor();
            if (Objects.isNull(editor)){
                return;
            }
            EditorSettings settings = editor.getSettings();
            if (settings.isUseSoftWraps()){
                settings.setUseSoftWraps(false);
                softWrapBtn.setBackground(JBColor.LIGHT_GRAY);
            }else {
                settings.setUseSoftWraps(true);
                softWrapBtn.setBackground(JBColor.DARK_GRAY);
                customEditor.setText(customEditor.getText());
                EditorEx editorEx = (EditorEx) editor;
                editorEx.setHorizontalScrollbarVisible(false);
                editorEx.getScrollPane().setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            }
        });
    }
}
